﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Modules.Library.Entities;
using System.Text.RegularExpressions;

namespace Modules.Core {
    public class BaseViewModel : INotifyPropertyChanged {

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public virtual void InitControlInfo() {

        }

        public virtual void DoClearFields() {

        }

        public virtual bool CheckIsNotNull() {
            return true;
        }

        public bool CheckDateFormat(string date, int typeOfDate) {
            string[] temp = new string[3];
            string number = "";
            int counter = 0;
            for(int i = 0; i < date.Length; i++) {
                if(Char.IsDigit(date[i])) {
                    number += date[i];
                    if(i == date.Length - 1) {
                        temp[counter] += number;
                    }
                }
                else {
                    temp[counter] = number;
                    number = "";
                    counter++;
                }
            }
            int day, month, year;
            switch(typeOfDate)
            {
                case Converter.DDMMYYYY:
                    {
                        day = Convert.ToInt32(temp[0]);
                        month = Convert.ToInt32(temp[1]);
                        year = Convert.ToInt32(temp[2]);
                        return CheckDate(day,month, year);
                    }
                case Converter.YYYYMMDD:
                    {
                        day = Convert.ToInt32(temp[2]);
                        month = Convert.ToInt32(temp[1]);
                        year = Convert.ToInt32(temp[0]);
                        return CheckDate(day, month, year);
                    }
                default://MMDDYYYY
                    {
                        day = Convert.ToInt32(temp[1]);
                        month = Convert.ToInt32(temp[0]);
                        year = Convert.ToInt32(temp[2]);
                        return CheckDate(day, month, year);
                    }
            }
        }

        public bool CheckUserNameFormat(string userName) {
            if(userName.Length > 20 || userName.Length <= 4)
                return false;
            else {
                for(int i = 0; i < userName.Length; i++) {
                    if(!IsCorrectChar(userName[i]))
                        return false;
                }
            }
            return true;
        }

        public bool CheckPasswordFormat(string password) {
            if(password.Length <= 4 || password.Length > 32)
                return false;
            else {
                for(int i = 0; i < password.Length; i++) {
                    if(!IsCorrectChar(password[i]))
                        return false;
                }
            }
            return true;
        }

        private bool CheckDate(int day, int month, int year) {
            if(day <= 0 && day > 31 || year < 0)
                return false;
            else {
                switch(month) {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        if(day > 0 && day <= 31) {
                            return true;
                        }

                        break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                        if(day > 0 && day <= 30) {
                            return true;
                        }

                        break;
                    case 2:
                        if(CheckYear(year)) {
                            if(day > 0 && day <= 29) {
                                return true;
                            }
                        }
                        else {
                            if(day > 0 && day <= 28) {
                                return true;
                            }
                        }

                        break;
                }
            }
            return false;
        }

        private bool CheckYear(int year) {
            if(year % 4 != 0) {
                return false;
            }
            else if(year % 400 == 0) {
                return true;
            }
            else if(year % 100 == 0) {
                return false;
            }
            else {
                return true;
            }
        }

        private bool IsCorrectChar(char ch) {
            string rules = @"[a-z]|[A-Z]|[0-9]";
            Regex regex = new Regex(rules);

            if(regex.IsMatch(ch.ToString())) {
                return true;
            }
            else {
                return false;
            }
        }
    }
}
